package cn.zhoutao.basic.util;

import cn.zhoutao.basic.jwt.JwtUtils;
import cn.zhoutao.basic.jwt.LoginResult;
import cn.zhoutao.basic.jwt.Payload;
import cn.zhoutao.basic.jwt.RsaUtils;
import cn.zhoutao.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * @Author:MrZhou
 * @Date:2022/6/25 18:13
 *
 * 登陆上下文：
 *
 * 当前登录人信息
 *当前登录人的权限
 */
public class LoginContext {
    /**
     * 获取当前登录人
     * @param request
     * @return
     */
    public static Logininfo getCurrentLogininfo(HttpServletRequest request) {
        //从请求头中获取u-token
        String token = request.getHeader("U-TOKEN");
        //获取公钥
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        //使用公钥去解密(payload:载荷)

        Payload<LoginResult> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginResult.class);
        LoginResult loginResult = payload.getUserInfo();
        return loginResult.getLogininfo();
    }
}
